package com.youi.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * json封装类
 */
public class JsonUtil {

    public static Gson GSON = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    public static <T> T fromJson(String src, java.lang.reflect.Type type) {
        return GSON.fromJson(src, type);
    }

    public static <T> T fromJson(JsonElement src, java.lang.reflect.Type type) {
        return GSON.fromJson(src, type);
    }


    /**
     * 读取json文件
     *
     * @return
     */
    public static String getJosnobject() {
        InputStream input = JsonUtil.class.getResourceAsStream("/accesstoken.json");
        try {
            StringBuilder sb = new StringBuilder();
            byte[] buffer = new byte[1024];
            int length = 0;
            length = input.read(buffer);
            while (length != -1) {
                sb.append(new String(buffer, 0, length));
                length = input.read(buffer);
            }
            String jsonString = sb.toString();
            return jsonString;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    //解析数组json
    public static List fromArrayJson2List(String src, Class cla) {
        List list = null;
        //Json的解析类对象
        JsonParser parser = new JsonParser();
        //将JSON的String 转成一个JsonArray对象
        JsonArray jsonArray = parser.parse(src).getAsJsonArray();
        if (jsonArray.size() > 0) {
            list = new ArrayList();
            for (JsonElement jsonElement : jsonArray) {
                list.add(fromJson(jsonElement, cla));
            }
        }
        return list;
    }

    /**
     * 对象转json
     *
     * @param o
     * @return
     */
    public static String toJson(Object o) {
//    	String jsonString=GSON.toJson(o);
        String jsonString = JSON.toJSONString(o, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteDateUseDateFormat);
        LogUtil.Debug("返回数据===" + jsonString);
        LogUtil.Debug("======================");
        return jsonString;
    }

    /**
     * 返回json
     *
     * @param response
     * @param o
     * @return
     */
    public static void toJson(HttpServletResponse response, Object o) {
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json; charset=utf-8");
            response.getWriter().print(GSON.toJson(o));
            response.setStatus(200);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * code msg 放入map 转json
     *
     * @param code
     * @param msg
     * @return
     */
    public static String toJson(Integer code, String msg) {
        ModelMap modelMap = new ModelMap();
        modelMap.put(CommonInfoUtil.JSONCODE, code);
        modelMap.put(CommonInfoUtil.JSONMSG, msg);
        return toJson(modelMap);
    }

    /**
     * code msg 放入map 转json
     *
     * @param code
     * @param msg
     * @return
     */
    public static String toJson(Integer code, String msg, Boolean flag) {
        ModelMap modelMap = new ModelMap();
        modelMap.put(CommonInfoUtil.JSONCODE, code);
        modelMap.put(CommonInfoUtil.JSONMSG, msg);
        modelMap.put("success", flag);
        return toJson(modelMap);
    }

    /**
     * code msg object 放入map 转json
     *
     * @param code
     * @param msg
     * @return
     */
    public static String toJson(Integer code, String msg, Object o) {
        ModelMap modelMap = new ModelMap();
        modelMap.put(CommonInfoUtil.JSONCODE, code);
        modelMap.put(CommonInfoUtil.JSONMSG, msg);
        modelMap.put(CommonInfoUtil.DATA, o);
        return toJson(modelMap);
    }

    /**
     * code msg 放入map 转json
     *
     * @param code
     * @param msg
     * @return
     */
    public static String toJson(Integer code, String msg, ModelMap modelMap) {
        modelMap.put(CommonInfoUtil.JSONCODE, code);
        modelMap.put(CommonInfoUtil.JSONMSG, msg);
        return toJson(modelMap);
    }


    public static void toJsonp(HttpServletResponse response, String callback, Object o) {  //返回jsonp格式数据
        try {
            PrintWriter out = response.getWriter();
            out.println(callback + "(" + GSON.toJson(o) + ")");
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static List removeRepetition(List lists) {    //去重复
        HashSet h = new HashSet(lists);
        lists.clear();
        lists.addAll(h);
        return lists;
    }


}
